package com.sc.adminspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.adminspringboot.pojo.Comments;
import com.sc.adminspringboot.service.ICommentsService;
import com.sc.adminspringboot.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 帖子评论及回复表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2025-09-20 02:47:56
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    ICommentsService commentsService;

    @RequestMapping("list")
    ResponseMsg commentsList(@RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "20") int limit,
                             @RequestParam(required = false) String searchKey,
                             @RequestParam(required = false) String searchValue,
                             @RequestParam(required = false) String sortBy,
                             @RequestParam(required = false) String sortOrder){
        try {
            IPage<Comments> pw=new Page<>(page,limit);
            QueryWrapper<Comments> qw= new QueryWrapper<>();

            // 添加搜索条件
            if (searchKey != null && !searchKey.isEmpty() && searchValue != null && !searchValue.isEmpty()) {
                switch (searchKey) {
                    case "commentId":
                        // 评论ID精确匹配
                        try {
                            qw.eq("comment_id", Long.parseLong(searchValue));
                        } catch (NumberFormatException e) {
                            // 如果不是有效数字，不添加条件
                        }
                        break;
                    case "content":
                        // 内容模糊搜索
                        qw.like("content", searchValue);
                        break;
                    default:
                        break;
                }
            }

            // 添加排序条件
            if (sortBy != null && !sortBy.isEmpty()) {
                if ("createTime".equals(sortBy)) {
                    if ("desc".equals(sortOrder)) {
                        qw.orderByDesc("create_time");
                    } else {
                        qw.orderByAsc("create_time");
                    }
                } else if ("isDeleted".equals(sortBy)) {
                    if ("desc".equals(sortOrder)) {
                        qw.orderByDesc("is_deleted");
                    } else {
                        qw.orderByAsc("is_deleted");
                    }
                } else if ("commentId".equals(sortBy)) {
                    if ("desc".equals(sortOrder)) {
                        qw.orderByDesc("comment_id");
                    } else {
                        qw.orderByAsc("comment_id");
                    }
                }
            } else {
                // 默认按评论ID升序排列
                qw.orderByAsc("comment_id");
            }

            IPage<Comments> commentsPage=commentsService.page(pw,qw);

            // 返回结构化数据
            Map<String, Object> result = new HashMap<>();
            result.put("records", commentsPage.getRecords());
            result.put("total",commentsPage.getTotal());
            result.put("size", commentsPage.getSize());
            result.put("current", commentsPage.getCurrent());
            result.put("pages", commentsPage.getPages());

            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查询成功",result);
        }catch (Exception e){
            return ResponseMsg.getInstance(ResponseMsg.ERROR,"查询失败: " + e.getMessage(), null);
        }
    }

    @RequestMapping("removeByNo")
    ResponseMsg del(Integer commentId){
        try {
            boolean result = commentsService.removeById(commentId);
            if (result) {
                return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "删除成功", null);
            } else {
                return ResponseMsg.getInstance(ResponseMsg.NOT_FOUND, "评论不存在", null);
            }
        } catch (Exception e) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "删除失败: " + e.getMessage(), null);
        }
    }

    @RequestMapping("removes")
    ResponseMsg delMany(@RequestBody List<Integer> commentIds){
        try {
            if (commentIds == null || commentIds.isEmpty()) {
                return ResponseMsg.getInstance(ResponseMsg.PARAM_ERROR, "评论ID列表不能为空", null);
            }

            boolean result = commentsService.removeByIds(commentIds);
            if (result) {
                return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "批量删除成功", null);
            } else {
                return ResponseMsg.getInstance(ResponseMsg.ERROR, "批量删除失败", null);
            }
        } catch (Exception e) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "批量删除失败: " + e.getMessage(), null);
        }
    }
}
